﻿using Microsoft.AspNetCore.DataProtection.Repositories;
using System.Xml.Linq;

namespace Web.Common
{
    /// <summary>
    /// 多应用实现Cookie共享
    /// 使用自定义的key加密cookies,解决负载均衡(集群)Forms认证问题
    /// </summary>
    public class XmlRepository : IXmlRepository
    {
        /// <summary>
        /// 路径
        /// </summary>
        private readonly string _KeyContentPath = "";

        /// <summary>
        /// 构造函数
        /// </summary>
        public XmlRepository()
        {
            string _path = Directory.GetCurrentDirectory();
            _KeyContentPath = Path.Combine(_path, "ShareKeys", "key-90b8ad9f-08c1-410e-8ffd-9b38a881fa67.xml");
        }

        /// <summary>
        /// 加载key信息，获取数据项
        /// </summary>
        /// <returns></returns>
        public IReadOnlyCollection<XElement> GetAllElements()
        {
            //加载key信息
            var elements = new List<XElement>() { XElement.Load(_KeyContentPath) };
            return elements;
        }

        /// <summary>
        /// 存储
        /// </summary>
        /// <param name="element"></param>
        /// <param name="friendlyName"></param>
        public void StoreElement(XElement element, string friendlyName)
        {
            //本文忽略实现存储功能，因为我们只需要读取已存在的Key即可
        }
    }
}
